package com.soxing.braindata.common.util;


import com.soxing.braindata.common.config.SystemConfig;
import com.soxing.braindata.dao.entity.UserInfo;
import com.soxing.braindata.domain.constants.SystemConstant;
import com.soxing.braindata.domain.vo.UserVo;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author :durango
 * @date :2022/7/24 21:30
 * @description web帮助类
 */
@Component
public class WebHelper {

    private static WebHelper webHelper;

    @Resource
    private SystemConfig systemConfig;

    @PostConstruct
    public void init() {
        webHelper = this;
    }



    /**
     * 获取http请求
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserVo getUserInfo() {
        boolean needLogin = webHelper.systemConfig.isNeedLogin();
        if (needLogin) {
            HttpServletRequest request = getRequest();
            return (UserVo) request.getSession().getAttribute(SystemConstant.LOGIN_USER);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("林浩");
        userInfo.setUserNum("zhangsan");
        userInfo.setSiteCode("001");
        return UserVo.builder().userNum("zhangsan").userInfo(userInfo).roleList(null).build();
    }

    /**
     * 获取中心编码
     *
     * @return
     */
    public static String getSitePkId() {
        UserVo userVo = getUserInfo();
        return userVo.getUserInfo().getSiteCode();
    }

    /**
     * 获取用户编号
     *
     * @return
     */
    public static String getUserNum() {
        UserVo userVo = getUserInfo();
        return userVo.getUserInfo().getUserNum();
    }
}
